package com.ysx.aspect;

import com.ysx.service.BlogService;
import com.ysx.utils.RedisUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 通过aop用redis更新浏览量
 */
@Aspect
@Component
public class RedisAspect {
    @Resource
    private  BlogService blogService;
    @Resource
    private  RedisUtil redisUtil;

    public RedisAspect(BlogService blogService, RedisUtil redisUtil) {
        this.blogService = blogService;
        this.redisUtil = redisUtil;
    }

    @Pointcut("execution(public * com.ysx.service.Impl.BlogServiceImpl.getDetailedBlog(..))")
    public void myPointCut(){}

    //在这个方法执行后
    @After("myPointCut()")
    public void doAfter(JoinPoint joinPoint) throws Throwable {
        Object[] objs=joinPoint.getArgs();
        Long id=(Long) objs[0];
        //根据id更新浏览量
        redisUtil.zIncrementScore("views",id.toString(),1);
    }
}